package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.Constant;
import cc.yugu.yhdw.common.domain.hdw.BatteryReportLog;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrder;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.hdw.BatteryReportLogService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangeOrderService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/security/hdw/battery_report_log")
public class BatteryReportLogController extends SecurityController {
    @Autowired
    BatteryReportLogService batteryReportLogService;
    @Autowired
    ExchangeOrderService exchangeOrderService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_08.getValue());
        model.addAttribute("flagBatteryId", 0);
    }

    @RequestMapping(value = "page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(BatteryReportLog batteryReportLog, String flagBatteryId) {
        // 跳转的加载
        if (batteryReportLog.getBatteryId() == null) {
            if ("0".equals(flagBatteryId)) {
                batteryReportLog.setBatteryId(null);
            }
            else {
                batteryReportLog.setBatteryId(flagBatteryId);
            }
        }

        return PageResult.successResult(batteryReportLogService.findPage(batteryReportLog));
    }

    @RequestMapping(value = "select_index.htm")
    public String selectIndex(Model model, String orderId, Date startDate, Date endDate) {
        model.addAttribute("orderId", orderId);
        model.addAttribute("startDate", startDate != null ? DateFormatUtils.format(startDate, Constant.DATE_TIME_FORMAT) : startDate);
        model.addAttribute("endDate", endDate != null ? DateFormatUtils.format(endDate, Constant.DATE_TIME_FORMAT) : endDate);
        return "/security/hdw/battery_report_log/select_index";
    }
    @ResponseBody
    @RequestMapping("select_page.htm")
    @ViewModel(ViewModel.JSON)
    public PageResult selectPage(BatteryReportLog search) {
        ExchangeOrder entity = exchangeOrderService.find(search.getOrderId());
        if (StringUtils.isNotEmpty(search.getOrderId()) &&entity!=null&& entity.getCreateTime() != null) {
            String suffix = DateFormatUtils.format(entity.getCreateTime(), "yyyyMM");
            search.setSuffix(suffix);
            return PageResult.successResult(batteryReportLogService.findselectPage(search));
        } else {
            return PageResult.emptyResult();
        }
    }
    @ResponseBody
    @RequestMapping("find_all_map_count.htm")
    @ViewModel(ViewModel.JSON)
    public ExtResult findAllMapCount(String orderId) {
        ExchangeOrder entity = exchangeOrderService.find(orderId);
        if (entity!=null&& entity.getCreateTime() != null) {
            return batteryReportLogService.findAllMapCount(orderId, entity.getCreateTime());
        } else {
            return ExtResult.failResult("");
        }
    }

    @ResponseBody
    @RequestMapping("update_address.htm")
    @ViewModel(ViewModel.JSON)
    public ExtResult updateAddress(BatteryReportLog batteryReportLog) {
        ExchangeOrder entity = exchangeOrderService.find(batteryReportLog.getOrderId());
        if (batteryReportLog.getCreateTime() != null &&entity!=null&& entity.getCreateTime() != null) {
            String suffix = DateFormatUtils.format(entity.getCreateTime(), "yyyyMM");
            batteryReportLog.setSuffix(suffix);
            return batteryReportLogService.updateAddress(batteryReportLog);
        } else {
            return ExtResult.failResult("");
        }
    }
    /**
     * 电池管理-轨迹-查看-根据订单查询柜机
     * @Param [batteryReportLog]
     * @Return cc.yugu.yhdw.common.entity.json.DataResult
     * @Author zxj 2018/7/24
     */
    @RequestMapping("location.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public DataResult location(BatteryReportLog batteryReportLog) {
        //return DataResult.successResult(batteryReportLogService.find(batteryReportLog));
        /**
         * 查询经纬度业务重构
         * 1.find方法是一代查询电池日志上传表中的经纬度，电池上传日志是一个表
         * 2.findCoordinates方法是二代查询电池日志上传表中的经纬度，电池上传日志是分月存储表结构
         * 3.二代的处理方式中，业务处理会过滤重复的点数据
         */
        return DataResult.successResult(batteryReportLogService.findCoordinates(batteryReportLog));
    }
    /**
     * 电池管理跳转到电池日志上传(好像有安全隐患)(伪造请求可以突破权限）
     * @Param [model]
     * @Return void
     * @Author zxj 2018/8/29
     */
    @RequestMapping(value = "find_battery_id.htm")
    public String findBatteryId(Model model, String batteryId) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_08.getValue());
        model.addAttribute("batteryId", batteryId);
        model.addAttribute("flagBatteryId", batteryId);
        return "/security/hdw/battery_report_log/index";
        // return "/security/hdw/battery/select_battery";
    }
    
}
